/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ConsultaEnderecoDialog.java
 *
 * Created on 24/10/2011, 16:16:24
 */
package br.com.seguradora.view.dialog;

import br.com.seguradora.exception.PersistenciaException;
import br.com.seguradora.model.Grupo;
import br.com.seguradora.model.Rateio;
import br.com.seguradora.model.Veiculo;
import br.com.seguradora.model.constant.Mes;
import br.com.seguradora.service.GrupoService;
import br.com.seguradora.service.RateioService;
import br.com.seguradora.view.StateEntityWindow;
import com.towel.swing.table.ObjectTableModel;
import com.towel.swing.table.Resizer;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.text.DecimalFormat;
import java.util.Calendar;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComponent;
import javax.swing.JRootPane;
import javax.swing.JSpinner;
import javax.swing.KeyStroke;
import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableColumn;
import um.filteredtext.number.CurrencyNumberAdapter;

/**
 *
 * @author Diego
 */
public class ConsultaRateioDialog extends javax.swing.JDialog {
    
    private ObjectTableModel<Veiculo> tableModel;
    private final static String titleWindow = "Controle de Rateios";
    private StateEntityWindow stateEntity;
    private CurrencyNumberAdapter prejuizosFormat;
    private CurrencyNumberAdapter valorCotaFormat;
    private DecimalFormat df = new DecimalFormat("0.00");
    
    public ConsultaRateioDialog(java.awt.Frame parent, boolean modal, StateEntityWindow stateEntity) {
        super(parent, modal);
        initComponents();
        this.stateEntity = stateEntity;
        setTitle(titleWindow + stateEntity);
        setLocationRelativeTo(null);
        organizeWindow();
    }

    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        grupoCombo = new javax.swing.JComboBox();
        pesquisarButton = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        mesCombo = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        anoSpinner = new javax.swing.JSpinner();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        table = new javax.swing.JTable();
        countLabel = new javax.swing.JLabel();
        gerarBoletoButton = new javax.swing.JButton();
        imprimirButton = new javax.swing.JButton();
        sairButton = new javax.swing.JButton();
        rateioPanel = new javax.swing.JPanel();
        prejuizosLabel = new javax.swing.JLabel();
        totalCotasLabel = new javax.swing.JLabel();
        valorCotaLabel = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Opções de pesquisa"));

        jLabel1.setText("Grupo:");

        pesquisarButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/seguradora/resources/magnifier.png"))); // NOI18N
        pesquisarButton.setMnemonic('p');
        pesquisarButton.setText("Pesquisar");
        pesquisarButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                pesquisarButtonActionPerformed(evt);
            }
        });

        jLabel3.setText("Mês:");

        mesCombo.setModel(new DefaultComboBoxModel(Mes.values()));

        jLabel4.setText("Ano:");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(85, 85, 85)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel4)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(anoSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(grupoCombo, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(mesCombo, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(pesquisarButton)))
                .addContainerGap(408, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(mesCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(anoSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(grupoCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(pesquisarButton))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Resultado da pesquisa"));

        table.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        table.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        table.setShowHorizontalLines(false);
        table.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tableMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(table);

        countLabel.setText("Nenhum registro localizado");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 788, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(countLabel))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 364, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(countLabel))
        );

        gerarBoletoButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/seguradora/resources/add.png"))); // NOI18N
        gerarBoletoButton.setMnemonic('g');
        gerarBoletoButton.setText("Gerar Boleto");
        gerarBoletoButton.setToolTipText("Gerar boleto");
        gerarBoletoButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                gerarBoletoButtonActionPerformed(evt);
            }
        });

        imprimirButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/seguradora/resources/printer.png"))); // NOI18N
        imprimirButton.setMnemonic('i');
        imprimirButton.setText("Imprimir");

        sairButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/seguradora/resources/cross.png"))); // NOI18N
        sairButton.setMnemonic('s');
        sairButton.setText("Sair");
        sairButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sairButtonActionPerformed(evt);
            }
        });

        rateioPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Rateio"));

        prejuizosLabel.setFont(new java.awt.Font("Tahoma", 1, 11));
        prejuizosLabel.setForeground(new java.awt.Color(255, 51, 51));
        prejuizosLabel.setText("Total em prejuízos: R$ 0,00");
        prejuizosLabel.setEnabled(false);

        totalCotasLabel.setFont(new java.awt.Font("Tahoma", 1, 11));
        totalCotasLabel.setForeground(new java.awt.Color(0, 153, 0));
        totalCotasLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        totalCotasLabel.setText("Total de cotas por grupo: 0");
        totalCotasLabel.setEnabled(false);

        valorCotaLabel.setFont(new java.awt.Font("Tahoma", 1, 11));
        valorCotaLabel.setForeground(new java.awt.Color(51, 102, 255));
        valorCotaLabel.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        valorCotaLabel.setText("Valor por cota: R$ 0,00");
        valorCotaLabel.setEnabled(false);

        javax.swing.GroupLayout rateioPanelLayout = new javax.swing.GroupLayout(rateioPanel);
        rateioPanel.setLayout(rateioPanelLayout);
        rateioPanelLayout.setHorizontalGroup(
            rateioPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(rateioPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(prejuizosLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 224, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(totalCotasLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 244, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(valorCotaLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 237, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        rateioPanelLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {prejuizosLabel, totalCotasLabel, valorCotaLabel});

        rateioPanelLayout.setVerticalGroup(
            rateioPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(rateioPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(rateioPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(prejuizosLabel)
                    .addComponent(valorCotaLabel)
                    .addComponent(totalCotasLabel))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(rateioPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(gerarBoletoButton)
                        .addGap(61, 61, 61)
                        .addComponent(imprimirButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 464, Short.MAX_VALUE)
                        .addComponent(sairButton)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(rateioPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 7, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(gerarBoletoButton)
                    .addComponent(imprimirButton)
                    .addComponent(sairButton))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void gerarBoletoButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_gerarBoletoButtonActionPerformed
        
    }//GEN-LAST:event_gerarBoletoButtonActionPerformed
    
    private void tableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tableMouseClicked
        if (evt.getClickCount() == 2) {
            int row = table.getSelectedRow();
            if (row >= 0) {
                
            }
        }
    }//GEN-LAST:event_tableMouseClicked
    
    private void pesquisarButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pesquisarButtonActionPerformed
        findRateio((Mes) mesCombo.getSelectedItem(),
                Integer.parseInt(anoSpinner.getModel().getValue().toString()),
                (Grupo) grupoCombo.getSelectedItem());
    }//GEN-LAST:event_pesquisarButtonActionPerformed
            
    private void sairButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sairButtonActionPerformed
        dispose();
    }//GEN-LAST:event_sairButtonActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName());
        } catch (Exception ex) {
            System.out.println(ex);
        }
        //</editor-fold>
        java.awt.EventQueue.invokeLater(new Runnable() {
            
            public void run() {
                ConsultaRateioDialog dialog = new ConsultaRateioDialog(new javax.swing.JFrame(), true, StateEntityWindow.SEARCH);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JSpinner anoSpinner;
    private javax.swing.JLabel countLabel;
    private javax.swing.JButton gerarBoletoButton;
    private javax.swing.JComboBox grupoCombo;
    private javax.swing.JButton imprimirButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JComboBox mesCombo;
    private javax.swing.JButton pesquisarButton;
    private javax.swing.JLabel prejuizosLabel;
    private javax.swing.JPanel rateioPanel;
    private javax.swing.JButton sairButton;
    private javax.swing.JTable table;
    private javax.swing.JLabel totalCotasLabel;
    private javax.swing.JLabel valorCotaLabel;
    // End of variables declaration//GEN-END:variables

    private void organizeWindow() {
        try {
            configTextFieldsNumericos();
            
            Calendar calendar = Calendar.getInstance();
            int mes = calendar.get(Calendar.MONTH);
            mesCombo.setSelectedIndex(mes);
            
            int ano = calendar.get(Calendar.YEAR);
            SpinnerModel spinnerModel =
                    new SpinnerNumberModel(ano, //initial value
                    ano - 100, //min
                    ano + 100, //max
                    1);
            anoSpinner.setModel(spinnerModel);
            //Make the year be formatted without a thousands separator.
            anoSpinner.setEditor(new JSpinner.NumberEditor(anoSpinner, "#"));
            
            loadGruposInComboBox();
            
            tableModel = new ObjectTableModel<Veiculo>(Veiculo.class, "id,tipo,placa,associado,cotas,mensalidade,rateio");
            table.setModel(tableModel);
            
            TableColumn col0 = table.getColumnModel().getColumn(0);
            TableColumn col1 = table.getColumnModel().getColumn(1);
            TableColumn col2 = table.getColumnModel().getColumn(2);
            TableColumn col3 = table.getColumnModel().getColumn(3);
            TableColumn col4 = table.getColumnModel().getColumn(4);
            TableColumn col5 = table.getColumnModel().getColumn(5);
            TableColumn col6 = table.getColumnModel().getColumn(6);
            
            DefaultTableCellRenderer headerRenderer = new DefaultTableCellRenderer();
            headerRenderer.setHorizontalAlignment(SwingConstants.CENTER);
            
            DefaultTableCellRenderer cellRenderer = new DefaultTableCellRenderer();
            cellRenderer.setHorizontalAlignment(SwingConstants.CENTER);
            
            col0.setHeaderRenderer(headerRenderer);
            col1.setHeaderRenderer(headerRenderer);
            col2.setHeaderRenderer(headerRenderer);
            col3.setHeaderRenderer(headerRenderer);
            col4.setHeaderRenderer(headerRenderer);
            col5.setHeaderRenderer(headerRenderer);        
            col6.setHeaderRenderer(headerRenderer);
            
            col0.setCellRenderer(cellRenderer);
            col1.setCellRenderer(cellRenderer);
            col2.setCellRenderer(cellRenderer);
            col4.setCellRenderer(cellRenderer);
            col5.setCellRenderer(cellRenderer);
            col6.setCellRenderer(cellRenderer);
            
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
    }
    
    private void showVeiculos(List<Veiculo> veiculoList) {
        tableModel.setData(veiculoList);
        countRows();
    }
    
    private void countRows() {
        if (tableModel.isEmpty()) {
            countLabel.setText("Nenhum registro localizado");
        } else {
            int count = tableModel.getRowCount();
            if (count == 1) {
                countLabel.setText("1 registro localizado");
            } else {
                countLabel.setText(count + " registros localizados");
            }
        }
        fitColumns();
    }
    
    public StateEntityWindow getStateEntity() {
        return stateEntity;
    }
    
    private void findRateio(Mes mes, Integer ano, Grupo grupo) {
        try {
            RateioService rateioService = new RateioService();
            Rateio rateio = rateioService.pesquisarRateio(mes.getValue(), ano, grupo);
            showQuadroRateio(rateio);
        } catch (PersistenciaException pex) {
            System.out.println(pex.getMessage());
        }
    }
    
    private void findVeiculos(Grupo grupo, double valorPorCota) {
        try {
            RateioService rateioService = new RateioService();
            List<Veiculo> veiculoList = rateioService.pesquisarVeiculos(grupo, valorPorCota);
            showVeiculos(veiculoList);
        } catch (PersistenciaException pex) {
            System.out.println(pex.getMessage());
        }
    }
    
    private void fitColumns() {
        if (tableModel.isEmpty()) {
            return;
        }
        Resizer.fitAllColumns(table);
    }
    
    @Override
    protected JRootPane createRootPane() {
        ActionListener closeListener = new ActionListener() {
            
            public void actionPerformed(ActionEvent e) {
                sairButtonActionPerformed(e);
            }
        };
        KeyStroke stroke =
                KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
        JRootPane myRootPane = new JRootPane();
        myRootPane.registerKeyboardAction(
                closeListener, stroke, JComponent.WHEN_IN_FOCUSED_WINDOW);
        
        return myRootPane;
    }
    
    private void loadGruposInComboBox() {
        try {
            GrupoService grupoService = new GrupoService();
            grupoCombo.setModel(new DefaultComboBoxModel(grupoService.listarDesbloqueados().toArray()));
        } catch (PersistenciaException ex) {
            System.out.println(ex.getMessage());
        }
    }
    
    private void configTextFieldsNumericos() {
        prejuizosFormat = new CurrencyNumberAdapter(false);
        prejuizosLabel.addKeyListener(prejuizosFormat);
        prejuizosLabel.addFocusListener(prejuizosFormat);
        
        valorCotaFormat = new CurrencyNumberAdapter(false);
        valorCotaLabel.addKeyListener(valorCotaFormat);
        valorCotaLabel.addFocusListener(valorCotaFormat);
    }
    
    private void showQuadroRateio(Rateio rateio) {
        if (rateio != null) {
            prejuizosLabel.setText("Total em prejuízos: " + prejuizosFormat.getNumberFormat().formatNumber(rateio.getTotalPrejuizos()));
            totalCotasLabel.setText("Total de cotas por grupo: " + df.format(rateio.getTotalCotas()));
            valorCotaLabel.setText("Valor por cota: " + valorCotaFormat.getNumberFormat().formatNumber(rateio.getValorCota()));
            isEnableLabels(true);
            findVeiculos((Grupo) grupoCombo.getSelectedItem(), rateio.getValorCota());
        } else {
            prejuizosLabel.setText("Total em prejuízos: R$ 0,00");
            totalCotasLabel.setText("Total de cotas por grupo: 0");
            valorCotaLabel.setText("Valor por cota: R$ 0,00");
            isEnableLabels(false);
            findVeiculos((Grupo) grupoCombo.getSelectedItem(), 0d);
        }
        rateioPanel.setBorder(BorderFactory.createTitledBorder("Rateio grupo "
                + ((Grupo) grupoCombo.getSelectedItem()).getNome().toLowerCase()
                + " em " + ((Mes) mesCombo.getSelectedItem()).toString().toLowerCase()
                + " de " + Integer.parseInt(anoSpinner.getModel().getValue().toString())));
    }
    
    private void isEnableLabels(boolean enable) {
        prejuizosLabel.setEnabled(enable);
        totalCotasLabel.setEnabled(enable);
        valorCotaLabel.setEnabled(enable);
    }
}